package br.usjt.component.login.dao;

import static br.usjt.component.login.util.MessageUtil.MESSAGE_SEM_RESULTADO;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import br.com.usjt.component.commons.login.exceptions.NotResultException;
import br.usjt.component.login.facade.dao.UsuarioDao;
import br.usjt.component.login.model.Usuario;

public class UsuarioDaoImp extends GenericDaoImp<Usuario> implements UsuarioDao {

    private static final long serialVersionUID = 529251235383402071L;

    public UsuarioDaoImp() {
        super(Usuario.class);
    }

    @Override
    public Usuario findByCPF(final String cpf) {

        String jpql = "Select u from Usuario u where cpf = :cpf";

        Query query = getEM().createQuery(jpql);
        query.setParameter("cpf", cpf);

        try {
            return (Usuario) query.getSingleResult();
        } catch (NoResultException nr) {
            throw new NotResultException(MESSAGE_SEM_RESULTADO, nr);
        }

    }

    @Override
    public Usuario findByEmail(final String email) {
        String jpql = "Select u from Usuario u where email = :email";

        Query query = getEM().createQuery(jpql);
        query.setParameter("email", email);

        try {
            return (Usuario) query.getSingleResult();
        } catch (NoResultException nr) {
            throw new NotResultException(MESSAGE_SEM_RESULTADO, nr);
        }
    }

}
